/*
 * MIT License
 * 
 * Copyright (c) 2017 wen.gu <454727014@qq.com>
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

/***************************************************************************
 * Name: tiny_queue.c
 *
 * Purpose: tiny queue data structure and API
 *
 * Developer:
 *   wen.gu , 2017-03-21
 *
 * TODO: 
 *
 ***************************************************************************/

#include <string.h>

#include "general_macro.h"
#include "tiny_queue.h"
#include "tiny_list.h"
#include "vos_mem.h"


/***************************************************************************
 *
 * macro declaration
 *
 ***************************************************************************/
#define LOGE printf
#define LOGD printf
#define LOGW printf
/***************************************************************************
 *
 * data structure declaration
 *
 ***************************************************************************/
/** data structure for item */
typedef struct my_queue_item_s
{
	tiny_list_node_t node;
	void* data;
}my_queue_item_t;

/** data structure for queue */
typedef struct my_queue_s
{
	tiny_list_t items;
}my_queue_t;

/***************************************************************************
 *
 * inner API define
 *
 ***************************************************************************/
static my_queue_item_t* tqItemNew(void* data)
{
	my_queue_item_t* item = (my_queue_item_t*)memAlloc(sizeof(my_queue_item_t));

	if (item)
	{
		item->data = data;
	}

	return item;
}
/***************************************************************************
 *
 * API define
 *
 ***************************************************************************/
GPHD tfQueueCreate(void)
{
	my_queue_t* mq = (my_queue_t*)memAlloc(sizeof(my_queue_t));

	if (mq)
	{
		memset(mq, 0, sizeof(*mq));
		tfListInitialize(&(mq->items), memFree);
	}
	else
	{
		LOGE("alloc queue instance failed\n");
	}

	return mq;
}


void tfQueueDestroy(GPHD queue)
{
	if (queue)
	{
		my_queue_t* mq = (my_queue_t*)queue;
		tiny_list_t* tl = &(mq->items);
		GU32 left_count = tfListCount(tl);

		if (left_count > 0) 
		{
			LOGW("here have: %d items in queue, it's maybe lead to memory leak\n", left_count);
		}

		tfListClear(tl);

		memFree(queue);
	}
}


void tfQueueClear(GPHD queue, tfQueueItemFreeFunc_t item_free)
{
	if (queue)
	{
		my_queue_t* mq = (my_queue_t*)queue;
		tiny_list_t* tl = &(mq->items);
		if (item_free)
		{
			tiny_list_node_t* node = tfListFront(tl);

			while (node)
			{
				my_queue_item_t* mqi = (my_queue_item_t*)node;

				item_free(mqi->data);

				node = node->next;
			}
		}
		else
		{
			LOGW("item_free is NULL, so here wouldn't free item, it maybe lead to memory leak\n");
		}

		tfListClear(tl);
	}
}


GBOL tfQueueEnQueue(GPHD queue, void* item)
{
	GBOL ret = GFALSE;
	if (queue)
	{
		my_queue_t* mq = (my_queue_t*)queue;
		tiny_list_t* tl = &(mq->items);
		my_queue_item_t* mqi = tqItemNew(item);

		GCHECK(mqi);
		
		tfListPushBack(tl, (tiny_list_node_t*)mqi);
		/** todo, is need check push to list is successed ?? */
		ret = GTRUE;		
	}

	return ret;
}


void *tfQueueDeQueue(GPHD queue)
{
	void* item = NULL;
	if (queue)
	{
		my_queue_t* mq = (my_queue_t*)queue;
		tiny_list_t* tl = &(mq->items);
		my_queue_item_t* node = (my_queue_item_t*)tfListFront(tl);

		if (node)
		{
			item = node->data;
			tfListPopFront(tl); /** remove head node from list */
		}
	}

	return item;
}


GU32 tfQueueLength(GPHD queue)
{
	GU32 ret = 0;
	if (queue)
	{
		my_queue_t* mq = (my_queue_t*)queue;

		ret = tfListCount(&(mq->items));
	}

	return ret;
}




